# -*-coding:utf-8-*-
__author__ = 'wss'

 # 定义处理网格图的功能函数
def from_id_width(id, width):
    return (id % width, id //width)  # 将序号转变为横纵坐标

def draw_tile(graph, id, style, width):
    # style是包含'point_to'和'start'两个元素的字典
    r = "."
    if 'number' in style and id in style['number']:
        r = '%d' %style['number'][id]

    if 'point_to' in style and style['point_to'].get(id, None) is not None:
        (x1, y1) = id  # 出发结点
        (x2, y2) = style['point_to'][id]  # 目的节点
        if x2 == x1+1:
            r = ">"
        if x2 == x1-1:
            r = "<"
        if y2 == y1+1:
            r = "V"
        if y2 == y1-1:
            r = "^"
    if 'path' in style and id in style['path']:
        r = '@'
    if id in graph.walls:
        r = '#' * width
    if 'start' in style and len(style['start'])==1 and id == style['start']:
        r = 'A'
    elif 'start' in style and  len(style['start'])>1 and id in style['start']:
        r = 'A'
    if 'goal' in style and len(style['goal'])==1 and id == style['goal']:
        r = 'Z'
    elif 'goal' in style and  len(style['goal'])>1 and id in style['goal']:
        r = 'Z'
    return r

def draw_grid(graph, width=2, **style):  # style为字典
    for y in range(graph.height):
        for x in range(graph.width):
            print '%%-%ds' %width  % draw_tile(graph, (x,y), style, width),  # %- 左对齐？
        print '\r'

def reconstruct_path(came_from, start, goal):
    current = goal
    path = []
    while current != start:
        path.append(current)
        current = came_from[current]
    path.append(start)
    path.reverse()
    return path